home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / disk / optim / PFSDefragTry.lha / RCS / PFSDefragTry.e next >
Encoding:
Text File  |  1999-10-08  |  11.7 KB  |  504 lines

  1. head    1.5;
  2. access;
  3. symbols;
  4. locks;
  5. comment    @# @;
  6.  
  7.  
  8. 1.5
  9. date    99.10.08.18.15.35;    author helios;    state Exp;
  10. branches;
  11. next    1.4;
  12.  
  13. 1.4
  14. date    99.10.08.17.35.31;    author helios;    state Exp;
  15. branches;
  16. next    1.3;
  17.  
  18. 1.3
  19. date    99.09.29.12.23.22;    author helios;    state Exp;
  20. branches;
  21. next    1.2;
  22.  
  23. 1.2
  24. date    99.09.29.12.15.27;    author helios;    state Exp;
  25. branches;
  26. next    1.1;
  27.  
  28. 1.1
  29. date    99.09.29.10.47.55;    author helios;    state Exp;
  30. branches;
  31. next    ;
  32.  
  33.  
  34. desc
  35. @PFSDefragTry Amiga E Source
  36. Tries to defragment files on a PFS volume using the
  37. PFS built-in defragment on overwrite feature.
  38. @
  39.  
  40.  
  41. 1.5
  42. log
  43. @- now I have it... hmmm... Stefan stored the pointers to the
  44.   comment, the filedate and the protection bits to variables
  45.   and freed the DosObject afterwards and then used them to
  46.   restore the file data... this can't work or only works
  47.   in the case when the freed structure has not been overwritten
  48.   again
  49.   AH this, sucked ;-)
  50.   hopefully it really copies filenotes now... now keeps
  51.   fib open till not used anymore
  52. - added some more comments to the source
  53. @
  54. text
  55. @/*
  56.     PFSDefragTry
  57.     $Id: PFSDefragTry.e 1.4 1999/10/08 17:35:31 helios Exp $
  58. */
  59.  
  60. OPT OSVERSION=37
  61. OPT REG=5
  62.  
  63. MODULE 'dos/dos'
  64. MODULE 'exec/memory'
  65.  
  66. CONST MAXLINELEN=1000, BUFLEN=65536
  67. CONST TEMPFILELEN=100
  68.  
  69. ENUM OK,ER_BADARGS,ER_DISKVALID,ER_DELETE,ER_COPYING,ER_EXAMINE,ER_PROT,ER_DATE,ER_COMMENT,ER_NOLOCK,BREAK
  70. ENUM ARG_DEVICE,ARG_TEMPDIR,ARG_BUFLEN,NUMARGS
  71.  
  72. PROC delfile(file)
  73.   DEF i
  74.  
  75.   i := DeleteFile(file)
  76.   IF i = FALSE
  77.     i := SetProtection(file, 0)
  78.     IF i= TRUE
  79.       i := DeleteFile(file)
  80.       IF i = FALSE THEN PrintF('Deleting file "\s" failed!', file)
  81.     ELSE
  82.       PrintF('Removing delete protection from file "\s" failed!\n', file)
  83.     ENDIF
  84.   ENDIF
  85. ENDPROC i
  86.  
  87. PROC copyfile(from=NIL,to=NIL,buffer=NIL,buflen=0)
  88.   DEF ok=FALSE, ready=FALSE, readlen, writelen, fh_from=NIL, fh_to=NIL
  89.  
  90.   IF fh_from:=Open(from,MODE_OLDFILE)
  91.     IF fh_to:=Open(to,MODE_NEWFILE)
  92.       IF buffer
  93.         IF buflen
  94.           REPEAT
  95.             readlen:=Read(fh_from, buffer, buflen)
  96.             IF readlen<buflen THEN ready:=TRUE
  97.             IF readlen<>TRUE
  98.               IF readlen>0
  99.                 writelen:=Write(fh_to, buffer, readlen)
  100.               ELSE
  101.                 writelen:=0
  102.               ENDIF
  103.               IF writelen<>readlen
  104.                 ready:=TRUE
  105.               ELSE
  106.                 IF ready THEN ok:=TRUE
  107.               ENDIF
  108.             ENDIF
  109.           UNTIL ready=TRUE
  110.         ENDIF
  111.       ENDIF
  112.       Close(fh_to)
  113.     ENDIF
  114.     Close(fh_from)
  115.   ENDIF
  116. ENDPROC ok
  117.  
  118. PROC main() HANDLE
  119.   DEF fh_fragfiles=NIL,i,s[MAXLINELEN]:STRING,file[MAXLINELEN]:STRING
  120.   DEF rdargs=NIL, templ, args[NUMARGS]:LIST, buffer=NIL, buflen=BUFLEN
  121.   DEF tempfile[TEMPFILELEN]:STRING
  122.   DEF lock=NIL:LONG
  123.   DEF fib=NIL:PTR TO fileinfoblock
  124.   DEF examineok:LONG
  125.  
  126.   args:=[NIL,NIL,NIL]
  127.  
  128.   templ:='DEVICE/A,TEMPDIR,BUFLEN/N'; rdargs:=ReadArgs(templ,args,NIL)
  129.   IF rdargs=NIL THEN Raise(ER_BADARGS)
  130.  
  131.   IF args[ARG_BUFLEN]
  132.     buflen:=Long(args[ARG_BUFLEN])
  133.     IF buflen<4096 THEN buflen:=4096
  134.   ENDIF
  135.  
  136.   PrintF('Examing \s with DiskValid... ',args[ARG_DEVICE])
  137.   Flush(stdout)
  138.  
  139.   StringF(s,'diskvalid \s analyse >RAM:fragfiles',args[ARG_DEVICE])
  140.   i:=SystemTagList(s, NIL)
  141.   IF i<>0
  142.     PrintF('failed!\n')
  143.     Raise(ER_DISKVALID)
  144.   ENDIF
  145.   PrintF('ok.\n')
  146.  
  147.   IF fh_fragfiles:=Open('RAM:fragfiles',MODE_OLDFILE)
  148.  
  149.     PrintF('Allocating io buffer... ')
  150.     Flush(stdout)
  151.     buffer:=AllocVec(buflen,MEMF_PUBLIC)
  152.     IF buffer=NIL
  153.       PrintF('failed!\n')
  154.       Raise("MEM")
  155.     ENDIF
  156.     PrintF('ok.\n')
  157.  
  158.     IF args[ARG_TEMPDIR]
  159.       tempfile:=args[ARG_TEMPDIR]
  160.     ELSE
  161.       tempfile:=args[ARG_DEVICE]
  162.     ENDIF
  163.  
  164.     AddPart(tempfile,'---PFSDefragTry.tmp---',TEMPFILELEN)
  165.  
  166.     WHILE Fgets(fh_fragfiles,file,MAXLINELEN)
  167.       SetStr(file,StrLen(file))
  168.       IF StrCmp(file,'fragmented file',StrLen('fragmented file'))=TRUE
  169.  
  170.         MidStr(file,file,StrLen('fragmented file '))
  171.         i:=InStr(file,' fragments')
  172.         MidStr(file,file,0,i)
  173.         REPEAT
  174.           MidStr(file,file,0,StrLen(file)-1)
  175.           RightStr(s,file,1)
  176.         UNTIL StrCmp(s,'(')=TRUE
  177.         MidStr(file,file,0,StrLen(file)-1)
  178.         MidStr(file,file,0,StrLen(file)-1)
  179.  
  180.         PrintF('Trying to defragment "\s"... ',file)
  181.         Flush(stdout)
  182.  
  183.         /* check for ctrl */
  184.         IF CtrlC()
  185.           PrintF('\n***Break\n')
  186.           IF fh_fragfiles THEN Close(fh_fragfiles)
  187.           CleanUp(10)
  188.         ENDIF
  189.  
  190.         /* get file information */
  191.         examineok := FALSE
  192.         IF (lock := Lock(file, ACCESS_READ)) > 0
  193.           fib := AllocDosObject(DOS_FIB, NIL)
  194.           IF fib = 0
  195.             Raise('MEM')
  196.           ENDIF
  197.           IF Examine(lock, fib)
  198.             examineok := TRUE
  199.           ELSE
  200.             Raise(ER_EXAMINE)
  201.           ENDIF
  202.           UnLock(lock)
  203.         ELSE
  204.           Raise(ER_NOLOCK)
  205.         ENDIF
  206.  
  207.         /* try to copy file to temporary place */
  208.         i:=copyfile(file,tempfile,buffer,buflen)
  209.         IF i = TRUE
  210.           i:=delfile(file)
  211.           IF i = FALSE
  212.             Raise(ER_DELETE)
  213.           ENDIF
  214.  
  215.           /* copy or rename the file back to original place */
  216.           i:=Rename(tempfile,file)
  217.           IF i=FALSE
  218.             i:=copyfile(tempfile,file,buffer,buflen)
  219.             IF i = TRUE
  220.               delfile(tempfile)
  221.             ELSE
  222.               Raise(ER_COPYING)
  223.             ENDIF
  224.           ENDIF
  225.  
  226.           /* set protection */
  227.           i := SetProtection(file,fib.protection)
  228.           IF i = FALSE
  229.             Raise(ER_PROT)
  230.           ENDIF
  231.  
  232.           /* set file date */
  233.           i := SetFileDate(file,fib.datestamp)
  234.           IF i = FALSE
  235.             Raise(ER_DATE)
  236.           ENDIF
  237.  
  238.           /* set comment */
  239.           i := SetComment(file,fib.comment)
  240.           IF i = FALSE
  241.             Raise(ER_COMMENT)
  242.           ENDIF
  243.           PrintF('ok.\n')
  244.  
  245.           IF fib
  246.             FreeDosObject(DOS_FIB, fib)
  247.             fib := 0
  248.           ENDIF
  249.  
  250.         ELSE
  251.           PrintF('failed!\n')
  252.           delfile(tempfile)
  253.         ENDIF
  254.  
  255.       ENDIF
  256.     ENDWHILE
  257.   ENDIF
  258.   Raise(OK)
  259.  
  260. EXCEPT
  261.   SELECT exception
  262.     CASE ER_BADARGS;        PrintFault(IoErr(), 'PFSDefragTry')
  263.     CASE ER_DISKVALID;      PrintF('DiskValid failed!\n')
  264.                             PrintFault(IoErr(), 'PFSDefragTry')
  265.     CASE ER_DELETE;         PrintF('Could not delete "\s"!\n', file)
  266.                             PrintFault(IoErr(), 'PFSDefragTry')
  267.     CASE ER_COPYING;        PrintF('Could not copy temporary file "\s"!\n', tempfile)
  268.                             PrintFault(IoErr(), 'PFSDefragTry')
  269.     CASE ER_EXAMINE;        PrintF('Could not Examine() "\s"!', file)
  270.                             PrintFault(IoErr(), 'PFSDefragTry')
  271.     CASE ER_PROT;           PrintF('Could not set protection bits for "\s"!', file)
  272.                             PrintFault(IoErr(), 'PFSDefragTry')
  273.     CASE ER_DATE;           PrintF('Could not set file date for "\s"!', file)
  274.                             PrintFault(IoErr(), 'PFSDefragTry')
  275.     CASE ER_COMMENT;        PrintF('Could not set comment for "\s"!', file)
  276.                             PrintFault(IoErr(), 'PFSDefragTry')
  277.     CASE ER_NOLOCK;         PrintF('Could not lock file "\s"!', file)
  278.                             PrintFault(IoErr(), 'PFSDefragTry')
  279.     CASE "MEM";             PrintF('Not enough memory!\n')
  280.     DEFAULT;                PrintFault(exception,'PFSDefragtry')
  281.   ENDSELECT
  282.  
  283.   IF fib THEN FreeDosObject(DOS_FIB, fib)
  284.   IF buffer THEN FreeVec(buffer)
  285.   IF fh_fragfiles THEN Close(fh_fragfiles)
  286.   IF rdargs THEN FreeArgs(rdargs)
  287.  
  288. ENDPROC
  289. CHAR '$VER: PFSDefragTry 1.5 (8.10.1999)',0
  290.  
  291. @
  292.  
  293.  
  294. 1.4
  295. log
  296. @- cosmetical changes, revised error messages
  297. - tried to find not setting filenote problem... this
  298.   is strange. I could reproduce it when there was other
  299.   activity on disc but after some changes it seemed to
  300.   be gone
  301. - cause of that I added all error checking it can get
  302.   when comment can not be set it should really complain
  303.   now
  304. @
  305. text
  306. @d3 1
  307. a3 1
  308.     $Id: PFSDefragTry.e 1.3 1999/09/29 12:23:22 helios Exp $
  309. d12 2
  310. a13 1
  311. CONST MAXLINELEN=2000, BUFLEN=65536
  312. d15 1
  313. a15 1
  314. ENUM OK,ER_BADARGS,ER_DISKVALID,ER_DELETE,ER_COPYING,ER_EXAMINE,ER_PROT,ER_DATE,ER_COMMENT,BREAK
  315. d67 1
  316. a67 1
  317.   DEF tempfile[50]:STRING
  318. d70 1
  319. a70 3
  320.   DEF protection=NIL:LONG
  321.   DEF date:PTR TO datestamp
  322.   DEF comment[200]:STRING
  323. d82 1
  324. a82 1
  325.   PrintF('Examing \s with diskvalid... ',args[ARG_DEVICE])
  326. d110 1
  327. a110 1
  328.     AddPart(tempfile,'---PFSDefragTry.tmp---',50)
  329. d129 1
  330. d136 2
  331. d140 3
  332. d144 1
  333. a144 5
  334.             protection := fib.protection
  335.                   date := fib.datestamp
  336.                comment := fib.comment
  337.             FreeDosObject(DOS_FIB, fib)
  338.             UnLock(lock)
  339. d146 1
  340. a146 1
  341.               Raise(ER_EXAMINE)
  342. d148 3
  343. d153 1
  344. d161 1
  345. d173 1
  346. a173 1
  347.           i := SetProtection(file,protection)
  348. d179 1
  349. a179 1
  350.           i := SetFileDate(file,date)
  351. d185 3
  352. a187 6
  353.           IF StrLen(comment) > 0
  354.             PrintF('\nSetting comment\n')
  355.             i := SetComment(file,comment)
  356.             IF i = FALSE
  357.               Raise(ER_COMMENT)
  358.             ENDIF
  359. d191 5
  360. d221 3
  361. a223 1
  362.     CASE ER_COMMENT;        PrintF('Could not set comment "\s" for "\s"!', comment, file)
  363. d229 1
  364. d233 1
  365. d235 1
  366. a235 1
  367. CHAR '$VER: PFSDefragTry 1.4 (8.10.1999)',0
  368. @
  369.  
  370.  
  371. 1.3
  372. log
  373. @- changed some texts
  374. @
  375. text
  376. @d3 1
  377. a3 1
  378.     $Id$
  379. d12 1
  380. a12 1
  381. CONST MAXLINELEN=1000, BUFLEN=65536
  382. d14 1
  383. a14 1
  384. ENUM OK,ER_BADARGS,ER_DISKVALID,ER_DELETE,ER_RENAMING,BREAK
  385. d20 6
  386. a25 6
  387.   i:=DeleteFile(file)
  388.   IF i=FALSE
  389.     i:=SetProtection(file, 0)
  390.     IF i=TRUE
  391.       i:=DeleteFile(file)
  392.       IF i=FALSE THEN PrintF('deleting file "\s" failed!', file)
  393. d27 1
  394. a27 1
  395.       PrintF('removing delete protection from file "\s" failed!\n', file)
  396. a29 1
  397.  
  398. a60 1
  399.  
  400. d67 5
  401. a71 5
  402.   DEF lock=NIL:LONG,
  403.       fib=NIL:PTR TO fileinfoblock,
  404.       protection=NIL:LONG,
  405.       date:PTR TO datestamp,
  406.       comment[200]:STRING
  407. d83 1
  408. a83 1
  409.   PrintF('examing \s with diskvalid... ',args[ARG_DEVICE])
  410. d96 1
  411. a96 1
  412.     PrintF('allocating io buffer... ')
  413. d127 1
  414. a127 1
  415.         PrintF('trying to defragment "\s"... ',file)
  416. d136 13
  417. d150 4
  418. a153 10
  419.         IF i=TRUE
  420.           IF  (lock  := Lock(file, ACCESS_READ)) >0
  421.             fib := AllocDosObject(DOS_FIB, NIL)
  422.             IF Examine(lock, fib)
  423.               protection := fib.protection
  424.                     date := fib.datestamp
  425.                  comment := fib.comment
  426.               FreeDosObject(DOS_FIB, fib)
  427.               UnLock(lock)
  428.             ENDIF
  429. a154 2
  430.           i:=delfile(file)
  431.           IF i=FALSE THEN Raise(ER_DELETE)
  432. d159 1
  433. a159 1
  434.             IF i:=TRUE
  435. d162 22
  436. a183 1
  437.               Raise(ER_RENAMING)
  438. a185 3
  439.           SetProtection(file,protection)
  440.           SetFileDate(file,date)
  441.           IF StrLen(comment) > 0 THEN SetComment(file,comment)
  442. d187 1
  443. d196 1
  444. a197 1
  445.   Raise(OK)
  446. d200 1
  447. a200 1
  448.     CASE ER_BADARGS;        PrintF('PFSDefragTry (W) by Martin Steigerwald and Stefan Pucino\nBad arguments\n')
  449. d202 13
  450. a214 2
  451.     CASE ER_DELETE;         PrintF('Deleting file "\s" failed!\n',file)
  452.     CASE ER_RENAMING;       PrintF('Renaming temp file "\s" failed!\n',tempfile)
  453. d216 1
  454. a216 1
  455.     DEFAULT;                PrintFault(exception,'Error!\n')
  456. d223 1
  457. a223 1
  458. CHAR '$VER: PFSDefragTry 1.3 (29.9.1999)',0
  459. @
  460.  
  461.  
  462. 1.2
  463. log
  464. @- now under control of HWGRCS (Hel)
  465. - implemented CTRL-C check while copying (Max)
  466. - implemented support for filecomment, date and attributes (Max)
  467. - This tool is completely Y2K-free ;-)) (Max)
  468. @
  469. text
  470. @d178 1
  471. a178 1
  472.     CASE ER_BADARGS;        PrintF('PFSDefragTry 1.2 (29.9.1999)\nBad arguments\n')
  473. d190 1
  474. a190 1
  475. CHAR '$VER: PFSDefragTry 1.2 (29.9.1999)',0
  476. @
  477.  
  478.  
  479. 1.1
  480. log
  481. @Initial revision
  482. @
  483. text
  484. @d2 2
  485. a3 2
  486. $VER: PFSDefragTry.e 37.4 (19.9.1999) (W) by Martin Steigerwald
  487. Placed in Public domain!
  488. d14 1
  489. a14 1
  490. ENUM OK,ER_BADARGS,ER_DISKVALID,ER_DELETE,ER_RENAMING
  491. d69 5
  492. d132 6
  493. d140 10
  494. d162 3
  495. d178 1
  496. a178 1
  497.     CASE ER_BADARGS;        PrintF('PFSDefragTry 37.4 (W) 17.9.1999 by Martin Steigerwald.\nBad arguments\n')
  498. a186 1
  499.  
  500. d190 1
  501. a190 1
  502. CHAR '$VER: PFSDefragTry 37.4 (17.9.1999) (W) by Martin Steigerwald',0
  503. @
  504.